package com.house.controller.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.house.bean.*;
import com.house.service.HCuisineService;
import com.house.service.HDishService;
import com.house.service.HMasterService;
import com.house.service.HUserService;
import com.house.utils.ChangeTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

//师傅控制层
@RestController
public class MasterController {
    @Autowired
    private HMasterService hMasterService;
    @Autowired
    private HUserService hUserService;
    @Autowired
    private HDishService hDishService;
    @Autowired
    private HCuisineService hCuisineService;

    //添加师傅 就是修改用户表
    @RequestMapping(value = "/master/add" , method = RequestMethod.POST)
    public Object addConsumer(@RequestBody HUser hUser) throws ParseException {
        hUser.setMid((int) Calendar.getInstance().getTimeInMillis());
        return hUserService.updateById(hUser);
    }
    //查询师傅会做的菜 通过师傅编号
    @RequestMapping(value = "/master/selectCooking",method = RequestMethod.GET)
    public Object selectCooking(@RequestParam(value = "pn",defaultValue = "1") Integer pn,@RequestParam ("mid") int mid){
        System.out.println("--------------------ID:"+mid+"-------------");
        JSONObject jsonObject = new JSONObject();
        //分页查询数据
        Page<HDish> hDishPage = new Page<>(pn, 5);
        //分页查询结果
        Page<HDish> page = hDishService.page(hDishPage,new QueryWrapper<HDish>().eq("mid",mid));
//        System.out.println("==================="+page.getRecords());
        List<HDish> list = page.getRecords();
        List<MasterCooking> masterCookings = new ArrayList<>();
        for (int i = 0; i<list.size(); i++) {
//            HDish one = hDishService.getOne(new QueryWrapper<HDish>().eq("id", list.get(i).getCId()));
            HCuisine hCuisine = hCuisineService.getOne(new QueryWrapper<HCuisine>().eq("id",list.get(i).getCId()));
            MasterCooking masterCooking = new MasterCooking();
            masterCooking.setId(list.get(i).getId());//编号 就是h_dish 表中的编号
            masterCooking.setCname(hCuisine.getName());//菜系名 比如 川菜
            masterCooking.setName(list.get(i).getName());//菜名 番茄炒蛋
            masterCooking.setPhoto("null"); //照片 默认为null先 目前还没找到照片
            masterCooking.setIntroduction(list.get(i).getIntroduction());//简介
            masterCooking.setPrices(list.get(i).getPrices());//价格
            masterCookings.add(masterCooking);
        }
        jsonObject.put("records",masterCookings);
        jsonObject.put("size",page.getSize());
        jsonObject.put("total",page.getTotal());
        return jsonObject;
    }
    //修改师傅会做的菜
    @RequestMapping(value = "/master/update_dishAndmaster" , method = RequestMethod.POST)
    public Object updateDishAndersonMaster(@RequestBody  HDish hDish) throws ParseException {
        return hDishService.updateById(hDish);
    }
    //删除一个菜品
    @RequestMapping(value = "/master/delete_dish",method = RequestMethod.GET)
    public Object deleteDish(@RequestParam("id") int id){
        return hDishService.removeById(id);
    }
    //增加一个菜品
    @RequestMapping(value = "/master/insert_dish",method = RequestMethod.POST)
    public Object deleteDish(@RequestBody HDish hDish){
        return hDishService.save(hDish);
    }
}
